#include <stdio.h>
#include <math.h>
int main()
{
    __int64 k,n,x,t;
    scanf("%I64d",&k);
    while (k--)
    {
        scanf("%I64d",&n);
        x=int((-1+sqrt(1+n*8))/2);
        t=n-x*(x+1)/2;
        if (t==0)t=x;
        t%=9;
        if (t==0)t=9;
        printf("%I64d\n",t);
    }
    return 0;
}
